package com.prs.crm.action.document;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.warning.AutoCodeAction;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.archives.ArchivesManager;
import com.prs.crm.domain.archives.ArchivesType;
import com.prs.crm.domain.document.Draft;
import com.prs.crm.domain.document.DraftType;
import com.prs.crm.domain.flow.FlowNodePerson;
import com.prs.crm.domain.flow.FlowVerifyDetail;
import com.prs.crm.domain.flow.WorkFlow;
import com.prs.crm.domain.flow.WorkFlowAndPerson;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.receipt.ReceiptRegister;
import com.prs.crm.domain.warning.AutoCode;
import com.prs.crm.service.archives.ArchivesManagerService;
import com.prs.crm.service.archives.ArchivesTypeService;
import com.prs.crm.service.document.DraftService;
import com.prs.crm.service.document.DraftTypeService;
import com.prs.crm.service.flow.FlowNodePersonService;
import com.prs.crm.service.flow.FlowVerifyDetailService;
import com.prs.crm.service.flow.WorkFlowAndPersonService;
import com.prs.crm.service.flow.WorkFlowNodeService;
import com.prs.crm.service.flow.WorkFlowService;
import com.prs.crm.service.warning.AutoCodeService;

public class DraftAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private Draft draft;
	private DraftService draftService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private  String codeNum;
	private File upload;
	private String uploadFileName;
	private File uploads;
	private String uploadsFileName;

	private FlowVerifyDetail detail;
	private List<WorkFlow> workFlow;
	private WorkFlow workFlows;
	private WorkFlowService workflowService;
	private List<FlowNodePerson> nodePerson;
	private FlowNodePersonService flowNodePersonService;
	private WorkFlowNodeService workFlowNodeService;
	private FlowVerifyDetailService flowVerifyDetailService;
	private WorkFlowAndPerson workFlowAndPerson;
	private WorkFlowAndPersonService workFlowAndPersonService;
	private FlowVerifyDetail flowVerifyDetail;
	private ArchivesManager archivesManager;
	private ArchivesManagerService archivesManagerService;
	private ArchivesType archivesType;
	private ArchivesTypeService archivesTypeService;
	private Integer id;
	private AutoCodeService autoCodeService;
	private DraftTypeService draftTypeService;
	private List<WorkFlowAndPerson> workFlowAndPersons;
	private List<FlowVerifyDetail> flowVerifyDetails;
	public List<WorkFlowAndPerson> getWorkFlowAndPersons() {
		return workFlowAndPersons;
	}

	public void setWorkFlowAndPersons(List<WorkFlowAndPerson> workFlowAndPersons) {
		this.workFlowAndPersons = workFlowAndPersons;
	}

	public List<FlowVerifyDetail> getFlowVerifyDetails() {
		return flowVerifyDetails;
	}

	public void setFlowVerifyDetails(List<FlowVerifyDetail> flowVerifyDetails) {
		this.flowVerifyDetails = flowVerifyDetails;
	}

	public AutoCodeService getAutoCodeService() {
		return autoCodeService;
	}

	public void setAutoCodeService(AutoCodeService autoCodeService) {
		this.autoCodeService = autoCodeService;
	}

	public ArchivesManager getArchivesManager() {
		return archivesManager;
	}

	public void setArchivesManager(ArchivesManager archivesManager) {
		this.archivesManager = archivesManager;
	}

	public ArchivesManagerService getArchivesManagerService() {
		return archivesManagerService;
	}

	public void setArchivesManagerService(
			ArchivesManagerService archivesManagerService) {
		this.archivesManagerService = archivesManagerService;
	}

	public ArchivesType getArchivesType() {
		return archivesType;
	}

	public void setArchivesType(ArchivesType archivesType) {
		this.archivesType = archivesType;
	}

	public ArchivesTypeService getArchivesTypeService() {
		return archivesTypeService;
	}

	public void setArchivesTypeService(ArchivesTypeService archivesTypeService) {
		this.archivesTypeService = archivesTypeService;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public File getUploads() {
		return uploads;
	}

	public void setUploads(File uploads) {
		this.uploads = uploads;
	}

	public String getUploadsFileName() {
		return uploadsFileName;
	}

	public void setUploadsFileName(String uploadsFileName) {
		this.uploadsFileName = uploadsFileName;
	}

	public FlowVerifyDetail getDetail() {
		return detail;
	}

	public void setDetail(FlowVerifyDetail detail) {
		this.detail = detail;
	}

	public List<WorkFlow> getWorkFlow() {
		return workFlow;
	}

	public void setWorkFlow(List<WorkFlow> workFlow) {
		this.workFlow = workFlow;
	}

	public WorkFlow getWorkFlows() {
		return workFlows;
	}

	public void setWorkFlows(WorkFlow workFlows) {
		this.workFlows = workFlows;
	}

	public WorkFlowService getWorkflowService() {
		return workflowService;
	}

	public void setWorkflowService(WorkFlowService workflowService) {
		this.workflowService = workflowService;
	}

	public List<FlowNodePerson> getNodePerson() {
		return nodePerson;
	}

	public void setNodePerson(List<FlowNodePerson> nodePerson) {
		this.nodePerson = nodePerson;
	}

	public FlowNodePersonService getFlowNodePersonService() {
		return flowNodePersonService;
	}

	public void setFlowNodePersonService(
			FlowNodePersonService flowNodePersonService) {
		this.flowNodePersonService = flowNodePersonService;
	}

	public WorkFlowNodeService getWorkFlowNodeService() {
		return workFlowNodeService;
	}

	public void setWorkFlowNodeService(WorkFlowNodeService workFlowNodeService) {
		this.workFlowNodeService = workFlowNodeService;
	}

	public FlowVerifyDetailService getFlowVerifyDetailService() {
		return flowVerifyDetailService;
	}

	public void setFlowVerifyDetailService(
			FlowVerifyDetailService flowVerifyDetailService) {
		this.flowVerifyDetailService = flowVerifyDetailService;
	}

	public WorkFlowAndPerson getWorkFlowAndPerson() {
		return workFlowAndPerson;
	}

	public void setWorkFlowAndPerson(WorkFlowAndPerson workFlowAndPerson) {
		this.workFlowAndPerson = workFlowAndPerson;
	}

	public WorkFlowAndPersonService getWorkFlowAndPersonService() {
		return workFlowAndPersonService;
	}

	public void setWorkFlowAndPersonService(
			WorkFlowAndPersonService workFlowAndPersonService) {
		this.workFlowAndPersonService = workFlowAndPersonService;
	}

	public FlowVerifyDetail getFlowVerifyDetail() {
		return flowVerifyDetail;
	}

	public void setFlowVerifyDetail(FlowVerifyDetail flowVerifyDetail) {
		this.flowVerifyDetail = flowVerifyDetail;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public Draft getDraft() {
		return draft;
	}

	public void setDraft(Draft draft) {
		this.draft = draft;
	}

	public DraftService getDraftService() {
		return draftService;
	}

	public void setDraftService(DraftService draftService) {
		this.draftService = draftService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public DraftTypeService getDraftTypeService() {
		return draftTypeService;
	}

	public void setDraftTypeService(DraftTypeService draftTypeService) {
		this.draftTypeService = draftTypeService;
	}

	@Action("listDraft")
	public String listDraft() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Draft.class).add(
				Restrictions.eq("recorder.id", AppContext.getAccountContext()
						.currentUser().getId()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDraftService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editDraft")
	public String editDraft() {
		this.workFlow = this.getWorkflowService().getWorkFlow("公文发文流程");
		codeNum=getCode("FW");
		return SUCCESS;
	}

	@Action("viewDraft")
	public String viewDraft() {
		return SUCCESS;
	}

	@Action(value = "saveDraft", results = {
			@Result(name = "success", type = "redirect", location = "editDraft"),
			@Result(name = "error", type = "chain", location = "editDraft"), })
	public String saveDraft() {
		if (upload != null) {
			this.draft.setAttach(FileUtil.getInstance().createAttach(upload,
					uploadFileName));
		}
		draft.setState("未提交");
		draft.setAppState(0);
		this.getDraftService().save(draft);
		return SUCCESS;
	}

	@Action(value = "saveSendDraft", results = {
			@Result(name = "success", type = "redirect", location = "listDraft"),
			@Result(name = "error", type = "chain", location = "editDraft"), })
	public String saveSendDraft() {
		if (upload != null) {
			this.draft.setAttach(FileUtil.getInstance().createAttach(upload,
					uploadFileName));
		}
		draft.setState("审批中");
		draft.setOabox(0);
		// draft.setFlow("经理考核");
		draft.setAppState(1);
		this.getDraftService().save(draft);
		List<FlowNodePerson> fp = this.getFlowNodePersonService()
				.getFlowNodePerson(draft.getWorkFlow());
		for (FlowNodePerson op : fp) {
			WorkFlowAndPerson workFlowAndPerson = new WorkFlowAndPerson();
			workFlowAndPerson.setWorkFlow(op.getWorkFlow());
			workFlowAndPerson.setNodePersonId(op.getNodeperson_id());
			workFlowAndPerson.setFlowstate("1");// 审签状态
			workFlowAndPerson.setFlowsequen(op.getWorkFlowNode().getSequence()
					+ "");
			workFlowAndPerson.setType(op.getWorkFlowNode().getType() + "");
			String[] persons = op.getNodeperson_id().split(",");
			System.out.println(persons.length);
			workFlowAndPerson.setPersonNum(persons.length);
			workFlowAndPerson.setDraft(draft);
			this.getWorkFlowAndPersonService().save(workFlowAndPerson);
		}
		return SUCCESS;
	}

	@Action(value = "removeDraft", results = {
			@Result(name = "success", type = "redirect", location = "listDraft"),
			@Result(name = "error", type = "chain", location = "listDraft") })
	public String removeDraft() {
		this.getDraftService().remove(draft);
		return SUCCESS;
	}

	@Action(value = "saveFinish", results = {
			@Result(name = "success", type = "redirect", location = "listWaitMyApp"),
			@Result(name = "error", type = "chain", location = "editWaitMyApp") })
	public String saveFinish() {
		List<WorkFlowAndPerson> wf = this.getWorkFlowAndPersonService()
				.getWorkFlowAndPersonByDraft(draft.getWorkFlow(), draft);
		List<WorkFlowAndPerson> wfp = sequens(wf);
		for (WorkFlowAndPerson wff : wfp) {
			if ("1".equals(wff.getFlowstate())) {
				String[] personss = wff.getNodePersonId().split(",");
				for (String persons : personss) {
					System.out.println(persons);
					if ((AppContext.getAccountContext().currentUser().getId() + "")
							.equals(persons)) {
						FlowVerifyDetail details = new FlowVerifyDetail();
						if (uploads != null) {
							this.flowVerifyDetail.setAttach(FileUtil
									.getInstance().createAttach(uploads,
											uploadsFileName));
							details.setAttach(flowVerifyDetail.getAttach());
						}
						details.setWorkFlowAndPerson(wff);
						details.setExecutor(AppContext.getAccountContext()
								.currentUser());
						details.setResult(flowVerifyDetail.getResult());
						details.setOpinion(flowVerifyDetail.getOpinion());
						this.getFlowVerifyDetailService().save(details);
						if ("5".equals(wff.getType())) {// 如果该节点是结束节点时，审核完成
							if (wff.getPersonNum() == 1) {
								if (flowVerifyDetail.getResult() == 4) {
									wff.setFlowstate("2");
									wff.setPersonNum(wff.getPersonNum() - 1);
									this.getWorkFlowAndPersonService().submit(
											wff);
									draft.setAppState(4);
									draft.setFlow("审核完成");
									draft.setPersonAppProcess("");
									draft.setPersonAppState(2);
									this.getDraftService().submit(draft);
									this.setPrompt("提交成功");
									return SUCCESS;
								} else if (flowVerifyDetail.getResult() == 1) {
									wff.setFlowstate("2");
									wff.setPersonNum(wff.getPersonNum() - 1);
									this.getWorkFlowAndPersonService().submit(
											wff);
									draft.setAppState(2);
									draft.setShowState("2");
									draft.setPersonAppState(2);
									draft.setFlow("审核完成");
									draft.setPersonAppProcess("");
									this.getDraftService().submit(draft);
									this.setPrompt("提交成功");
									return SUCCESS;
								}
								break;
							} else {
								if (flowVerifyDetail.getResult() == 4) {
									wff.setFlowstate("2");
									wff.setPersonNum(wff.getPersonNum() - 1);
									this.getWorkFlowAndPersonService().submit(
											wff);
									draft.setAppState(4);
									draft.setPersonAppProcess("");
									draft.setPersonAppState(2);
									draft.setFlow("审核完成");
									this.getDraftService().submit(draft);
									this.setPrompt("提交成功");
									return SUCCESS;
								} else if (flowVerifyDetail.getResult() == 1) {
									wff.setPersonNum(wff.getPersonNum() - 1);
									this.getWorkFlowAndPersonService().submit(
											wff);
									draft.setAppState(1);
									draft.setPersonAppState(2);
									draft.setFlow(AppContext
											.getAccountContext().currentUser()
											.getResume().getName()
											+ "审核");
									if (draft.getPersonAppProcess() == null
											|| "".equals(draft
													.getPersonAppProcess()
													.trim())) {
										draft.setPersonAppProcess(AppContext
												.getAccountContext()
												.currentUser().getResume()
												.getName());
									} else {
										draft.setPersonAppProcess(draft
												.getPersonAppProcess()
												+ ","
												+ AppContext
														.getAccountContext()
														.currentUser()
														.getResume().getName());
									}

									this.getDraftService().submit(draft);
									this.setPrompt("提交成功");
									return SUCCESS;
								}
								break;
							}
						} else {
							if (wff.getPersonNum() == 1) {
								if (flowVerifyDetail.getResult() == 4) {
									wff.setFlowstate("2");
									wff.setPersonNum(wff.getPersonNum() - 1);
									this.getWorkFlowAndPersonService().submit(
											wff);
									draft.setAppState(4);
									draft.setFlow("审核完成");
									draft.setPersonAppProcess("");
									draft.setPersonAppState(2);
									this.getDraftService().submit(draft);
									this.setPrompt("提交成功");
									return SUCCESS;
								} else if (flowVerifyDetail.getResult() == 1) {
									wff.setFlowstate("2");
									wff.setPersonNum(wff.getPersonNum() - 1);
									this.getWorkFlowAndPersonService().submit(
											wff);
									draft.setAppState(1);
									draft.setShowState("2");
									draft.setPersonAppState(2);
									draft.setFlow(AppContext
											.getAccountContext().currentUser()
											.getResume().getName()
											+ "审核");
									draft.setPersonAppProcess("");
									this.getDraftService().submit(draft);
									this.setPrompt("提交成功");
									return SUCCESS;
								}
								break;
							} else {
								if (flowVerifyDetail.getResult() == 4) {
									wff.setPersonNum(wff.getPersonNum() - 1);
									this.getWorkFlowAndPersonService().submit(
											wff);
									draft.setAppState(4);
									System.out.println(draft.getAppState());
									draft.setFlow("审核完成");
									draft.setPersonAppProcess("");
									draft.setPersonAppState(2);
									this.getDraftService().submit(draft);
									this.setPrompt("提交成功");
									return SUCCESS;
								} else if (flowVerifyDetail.getResult() == 1) {
									wff.setPersonNum(wff.getPersonNum() - 1);
									this.getWorkFlowAndPersonService().submit(
											wff);
									draft.setShowState("2");
									draft.setFlow(AppContext
											.getAccountContext().currentUser()
											.getResume().getName()
											+ "审核");
									if (draft.getPersonAppProcess() == null
											|| "".equals(draft
													.getPersonAppProcess()
													.trim())) {
										draft.setPersonAppProcess(AppContext
												.getAccountContext()
												.currentUser().getResume()
												.getName());
									} else {
										draft.setPersonAppProcess(draft
												.getPersonAppProcess()
												+ ","
												+ AppContext
														.getAccountContext()
														.currentUser()
														.getResume().getName());
									}
									this.getDraftService().submit(draft);
									this.setPrompt("提交成功");
									return SUCCESS;
								}
								break;
							}
						}

					}
				}
			}
		}
		return SUCCESS;
	}

	// @Action(value="saveDisagree",results={
	// @Result(name="success",type="redirect",location="listWaitMyApp"),
	// @Result(name="error",type="chain",location="editWaitMyApp")
	// })
	// public String saveDisagree(){
	// draft.setState("未完成");
	// this.getDraftService().save(draft);
	// return SUCCESS;
	// }
	@Action("editWaitMyApp")
	public String editWaitMyApp() {
		return SUCCESS;
	}

	@Action("editRule")
	public String editRule() {
		this.workFlow = this.getWorkflowService().getWorkFlow("公文发文流程");
		codeNum=getCode("FW");
		return SUCCESS;
	}

	@Action("viewRule")
	public String viewRule() {
		return SUCCESS;
	}

	@Action("editInform")
	public String editInform() {
		this.workFlow = this.getWorkflowService().getWorkFlow("公文发文流程");
		codeNum=getCode("FW");
		return SUCCESS;
	}

	@Action("viewInform")
	public String viewInform() {
		return SUCCESS;
	}

	@Action("editProclamation")
	public String editProclamation() {
		this.workFlow = this.getWorkflowService().getWorkFlow("公文发文流程");
		codeNum=getCode("FW");
		return SUCCESS;
	}

	@Action("viewProclamation")
	public String viewProclamation() {
		return SUCCESS;
	}

	@Action("editConference")
	public String editConference() {
		this.workFlow = this.getWorkflowService().getWorkFlow("公文发文流程");
		codeNum=getCode("FW");
		return SUCCESS;
	}

	@Action("viewConference")
	public String viewConference() {
		return SUCCESS;
	}

	@Action("editCircular")
	public String editCircular() {
		this.workFlow = this.getWorkflowService().getWorkFlow("公文发文流程");
		codeNum=getCode("FW");
		return SUCCESS;
	}

	@Action("viewCircular")
	public String viewCircular() {
		return SUCCESS;
	}

	@Action("editInstructions")
	public String editInstructions() {
		this.workFlow = this.getWorkflowService().getWorkFlow("公文发文流程");
		codeNum=getCode("FW");
		return SUCCESS;
	}

	@Action("viewInstructions")
	public String viewInstructions() {
		return SUCCESS;
	}

	@Action("editGroup")
	public String editGroup() {
		this.workFlow = this.getWorkflowService().getWorkFlow("公文发文流程");
		codeNum=getCode("FW");
		return SUCCESS;
	}

	@Action("viewGroup")
	public String viewGroup() {
		return SUCCESS;
	}

	/*
	 * 待我审批
	 */
	@Action("listWaitMyApp")
	public String listWaitMyApp() {
		Person currentperson = AppContext.getAccountContext().currentUser();
		List<Draft> df = this.getDraftService().getAllDraft();
		for (Draft ob : df) {
			List<WorkFlowAndPerson> wf = this.getWorkFlowAndPersonService()
					.getWorkFlowAndPersonByDraft(ob.getWorkFlow(), ob);
			List<WorkFlowAndPerson> wff = sequens(wf);
			for (WorkFlowAndPerson obb : wff) {
				if ("1".equals(obb.getFlowstate())) {
					if ("1".equals(obb.getType())) {
						String[] personss = obb.getNodePersonId().split(",");
						for (String persons : personss) {
							if ((currentperson.getId() + "").equals(persons)) {
								// 如果相同则 需要该人审签
								// 设置是否显示
								if (ob.getPersonAppProcess() != null) {
									String[] personapp = ob
											.getPersonAppProcess().split(",");
									for (String pa : personapp) {
										System.out.println(pa);
										if (currentperson.getResume().getName()
												.equals(pa)) {
											ob.setPersonAppState(2);
											this.getDraftService().submit(ob);
											break;
										} else {
											ob.setShowState(AppContext
													.getAccountContext()
													.currentUser().getId()
													+ "：1");
											ob.setAppState(1);
											ob.setPersonAppState(1);
											this.getDraftService().submit(ob);
										}
									}
								} else {
									ob.setShowState(AppContext
											.getAccountContext().currentUser()
											.getId()
											+ "：1");
									ob.setAppState(1);
									ob.setPersonAppState(1);
									this.getDraftService().submit(ob);
								}
							} else {

							}
						}
						break;
					}
					if ("2".equals(obb.getType())) {
						String[] personss = obb.getNodePersonId().split(",");
						for (String persons : personss) {
							if ((currentperson.getId() + "").equals(persons)) {
								// 如果相同则 需要该人审签
								// 设置是否显示
								if (ob.getPersonAppProcess() != null) {
									String[] personapp = ob
											.getPersonAppProcess().split(",");
									for (String pa : personapp) {
										System.out.println(pa);
										if (currentperson.getResume().getName()
												.equals(pa)) {
											ob.setPersonAppState(2);
											this.getDraftService().submit(ob);
											break;
										} else {
											ob.setShowState(AppContext
													.getAccountContext()
													.currentUser().getId()
													+ "：1");
											ob.setAppState(1);
											ob.setPersonAppState(1);
											this.getDraftService().submit(ob);
										}
									}
								} else {
									ob.setShowState(AppContext
											.getAccountContext().currentUser()
											.getId()
											+ "：1");
									ob.setAppState(1);
									ob.setPersonAppState(1);
									this.getDraftService().submit(ob);
								}
							} else {

							}
						}
						break;
					}
					if ("3".equals(obb.getType())) {
						String[] personss = obb.getNodePersonId().split(",");
						for (String persons : personss) {
							if ((currentperson.getId() + "").equals(persons)) {
								// 如果相同则 需要该人审签
								// 设置是否显示
								if (ob.getPersonAppProcess() != null) {
									String[] personapp = ob
											.getPersonAppProcess().split(",");
									for (String pa : personapp) {
										System.out.println(pa);
										if (currentperson.getResume().getName()
												.equals(pa)) {
											ob.setPersonAppState(2);
											this.getDraftService().submit(ob);
											break;
										} else {
											ob.setShowState(AppContext
													.getAccountContext()
													.currentUser().getId()
													+ "：1");
											ob.setAppState(1);
											ob.setPersonAppState(1);
											this.getDraftService().submit(ob);
										}
									}
								} else {
									ob.setShowState(AppContext
											.getAccountContext().currentUser()
											.getId()
											+ "：1");
									ob.setAppState(1);
									ob.setPersonAppState(1);
									this.getDraftService().submit(ob);
								}
							} else {

							}
						}
						break;
					}
					if ("5".equals(obb.getType())) {
						String[] personss = obb.getNodePersonId().split(",");
						for (String persons : personss) {
							if ((currentperson.getId() + "").equals(persons)) {
								// 如果相同则 需要该人审签
								// 设置是否显示
								if (ob.getPersonAppProcess() != null) {
									String[] personapp = ob
											.getPersonAppProcess().split(",");
									for (String pa : personapp) {
										System.out.println(pa);
										if (currentperson.getResume().getName()
												.equals(pa)) {
											ob.setPersonAppState(2);
											this.getDraftService().submit(ob);
											break;
										} else {
											ob.setShowState(AppContext
													.getAccountContext()
													.currentUser().getId()
													+ "：1");
											ob.setPersonAppState(1);
											this.getDraftService().submit(ob);
										}
									}
								} else {
									ob.setShowState(AppContext
											.getAccountContext().currentUser()
											.getId()
											+ "：1");
									ob.setPersonAppState(1);
									this.getDraftService().submit(ob);
								}
							} else {

							}
						}
						break;
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Draft.class);
		criteria.add(Restrictions.isNotNull("workFlow"));
		criteria.add(Restrictions.eq("appState", 1));
		criteria.add(Restrictions.eq("personAppState", 1));
		criteria.add(Restrictions.eq("showState", AppContext
				.getAccountContext().currentUser().getId()
				+ "：1"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDraftService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	public static List<WorkFlowAndPerson> sequens(List<WorkFlowAndPerson> ss) {
		List<WorkFlowAndPerson> returnlist = new ArrayList<WorkFlowAndPerson>();
		int slength = ss.size();
		for (int i = 0; i < slength; i++) {

			for (WorkFlowAndPerson uo : ss) {
				if (uo.getFlowsequen().equals(i + "")) {
					returnlist.add(uo);
				}
			}
		}
		return returnlist;

	}

	/*
	 * 经我审批
	 */
	@Action("listPassMyApp")
	public String listPassMyApp() {
		Person currentperson = AppContext.getAccountContext().currentUser();
		List<Draft> df = this.getDraftService().getAllDraft();
		for (Draft ob : df) {
			List<WorkFlowAndPerson> wf = this.getWorkFlowAndPersonService()
					.getWorkFlowAndPersonByDraft(ob.getWorkFlow(), ob);
			List<WorkFlowAndPerson> wff = sequens(wf);
			for (WorkFlowAndPerson obb : wff) {
				if ("2".equals(obb.getFlowstate())) {
					String[] personss = obb.getNodePersonId().split(",");
					for (String persons : personss) {
						if ((currentperson.getId() + "").equals(persons)) {
							ob.setShowState(AppContext.getAccountContext()
									.currentUser().getId()
									+ "：1");
							this.getDraftService().submit(ob);
						} else {
							break;
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Draft.class);
		criteria.add(Restrictions.isNotNull("workFlow"));
		criteria.add(Restrictions.eq("showState", AppContext
				.getAccountContext().currentUser().getId()
				+ "：1"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDraftService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	// 分发
	@Action("editIssue")
	public String editIssue() {
		return SUCCESS;
	}

	@Action(value = "saveIssue", results = {
			@Result(name = "success", type = "redirect", location = "listDraft"),
			@Result(name = "error", type = "chain", location = "editIssue") })
	public String saveIssue() {
		draft.setIsReady("否");
		this.getDraftService().save(draft);
		return SUCCESS;
	}

	// 待我阅读
	@Action("listWaitMyReady")
	public String listWaitMyReady() {
		List<Draft> re=this.getDraftService().getAllDraft();
		for (Draft res : re) {
			if (res.getReadyPerson()==null || "".equals(res.getReadyPerson().trim())) {
				res.setShowReady("5");
				this.getDraftService().submit(res);
			}else{
				String[] reper=res.getReadyPerson().split(",");
				for (String rep : reper) {
					if (AppContext.getAccountContext().currentUser().getResume().getName().equals(rep)) {
						if (res.getAlreadyreadPerson()==null || "".equals(res.getAlreadyreadPerson().trim())) {
							res.setIsReady("否");
							res.setShowReady(AppContext.getAccountContext().currentUser().getId()+":1");
							this.getDraftService().submit(res);
							break;
						}else{
							String[] alPer=res.getAlreadyreadPerson().split(",");
							for (String alpers : alPer) {
								if (AppContext.getAccountContext().currentUser().getResume().getName().equals(alpers)) {
									res.setIsReady("是");
									res.setShowReady(AppContext.getAccountContext().currentUser().getId()+":1");
									this.getDraftService().submit(res);
									break;
								}else{
									res.setIsReady("否");
									res.setShowReady(AppContext.getAccountContext().currentUser().getId()+":1");
									this.getDraftService().submit(res);
								}
							}
						}
						break;
					}else{
						res.setShowReady("5");
						this.getDraftService().submit(res);
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Draft.class);
		criteria.add(Restrictions.eq("showReady", AppContext.getAccountContext().currentUser().getId()+":1"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDraftService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editWaitMyReady")
	public String editWaitMyReady() {
		return SUCCESS;
	}

	@Action(value = "saveIssueReady", results = {
			@Result(name = "success", type = "redirect", location = "listWaitMyReady"),
			@Result(name = "error", type = "chain", location = "editWaitMyReady") })
	public String saveIssueReady() {
		if (draft.getAlreadyreadPerson()==null ||"".equals(draft.getAlreadyreadPerson().trim())) {
			draft.setAlreadyreadPerson(AppContext.getAccountContext().currentUser().getResume().getName());
		}else{
			draft.setAlreadyreadPerson(draft.getAlreadyreadPerson()+","+AppContext.getAccountContext().currentUser().getResume().getName());
		}
		draft.setIsReady("是");
		this.getDraftService().submit(draft);
		return SUCCESS;
	}

	// 发文监控
	@Action("listMonitoring")
	public String listMonitoring() {
		Person person=AppContext.getAccountContext().currentUser();
		List<Draft> df=this.getDraftService().getAllDraft();
		for (Draft dfs : df) {
			List<DraftType> dt=this.getDraftTypeService().getDraftTypeByName(dfs.getDraftTypeName());
			for (DraftType dts : dt) {
				if (dts.getMonitoringPerson()==null || "".equals(dts.getMonitoringPerson().trim()) || "0".equals(dts.getMonitoringPerson().trim())) {
					dfs.setJkState(person.getId()+":1");
					this.getDraftService().submit(dfs);
				}else{
					String[] per=dts.getMonitoringPerson().split(",");
					for (String pers : per) {
						if (person.getResume().getName().equals(pers)) {
							dfs.setJkState(person.getId()+":1");
							this.getDraftService().submit(dfs);
							break;
						}else{
							dfs.setJkState("5");
							this.getDraftService().submit(dfs);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Draft.class);
		criteria.add(Restrictions.eq("oabox", 0));
		criteria.add(Restrictions.eq("jkState", person.getId()+":1"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDraftService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewMonitoring")
	public String viewMoitoring() {
		return SUCCESS;
	}

	@Action(value = "saveMoittoringFile", results = {
			@Result(name = "success", type = "redirect", location = "listMonitoring"),
			@Result(name = "error", type = "chain", location = "listMonitoring") })
	public String saveMoittoringFile() {
		if (ids != null) {
			this.draft = this.getDraftService().get(ids[0]);
		}
		if (draft.getAppState() != 2 && draft.getAppState() != 4) {
			this.addActionError("该任务还在审批中不能归档");
			return ERROR;
		}
		this.archivesType = this.archivesTypeService.get(id);
		ArchivesManager am = new ArchivesManager();
		am.setArchivesType(archivesType);
		am.setNumber("TRACIN" + new Date().getTime() + "V");
		am.setArchivesName(draft.getTheme());
		am.setDegree(draft.getType());
		am.setSendTime(draft.getRecordTime());
		am.setSendPerson(draft.getRecorder());
		am.setState(0);
		if (draft.getAttach() != null) {
			am.setAttach(draft.getAttach());
		}
		am.setMark(1);
		this.getArchivesManagerService().save(am);
		draft.setOabox(1);// 4为归档
		this.getDraftService().submit(draft);
		return SUCCESS;
	}

	public String getCodeNum() {
		return codeNum;
	}

	public void setCodeNum(String codeNum) {
		this.codeNum = codeNum;
	}
	/***
	 * 根据类型进行编码的保存
	 * 
	 * @param type
	 */
	public String getCode(String type) {
	    AutoCode alist=this.getAutoCodeService().getTaskRemind(type);
	    Date dat=new Date();
	    SimpleDateFormat adf=new SimpleDateFormat("yyyyMMdd");
	    String date=adf.format(dat);
	    if(alist==null)
	    {//初始的时候调用
		    AutoCode  ac=new AutoCode();
		    ac.setCodeType(type);
		    ac.setCodeValue(type+date+"0000001");
		    ac.setAddValue("0000001");
		    this.getAutoCodeService().save(ac);	
		    return type+date+"0000001";
	    }else{
	    	String vac=alist.getAddValue();
	    	AutoCode alistA=this.getAutoCodeService().getTaskRemind(type);
	    	alistA.setCodeValue(type+date+new AutoCodeAction().addCodes(vac));
	    	alistA.setAddValue(new AutoCodeAction().addCodes(vac));
		    this.getAutoCodeService().submit(alistA);	
		    return type+date+new AutoCodeAction().addCodes(vac);
	    }
	}
	@Action(value = "viewAppDraft", results = { @Result(name = "error", type = "chain", location = "listDraft") })
	public String viewAppDraft(){
		if (ids!=null) {
			draft=this.getDraftService().get(ids[0]);
			this.workFlowAndPersons=this.getWorkFlowAndPersonService().getWorkFlowAndPersonByDraft(draft.getWorkFlow(), draft);
			this.flowVerifyDetails=this.getFlowVerifyDetailService().getAllFlowVerifyDetails();
			return SUCCESS;
		}else{
			this.addActionError("请选择一行");
			return ERROR;
		}
			
	}
}
